
// pages/productList/productList.js
import { getSearchInfo } from '../../../utils/networkRequest.js'
import { errorMsg } from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    searchVal: "",//搜索内容
    classindex:["",""]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this; 
    if (options.productList) {
      let list = JSON.parse(options.productList);
      let item = JSON.parse(options.item); 

      wx.setNavigationBarTitle({
        title: item.title
      }) 
      console.log(list)
      self.setData({
        list: list,
        item:item,
        classindex: options.classindex
      })
    }
  },
  onShow:function(){
    this.onSearch();
  },
  //获取搜索内容
  getSearchVal: function (e) {
    this.setData({
      searchVal: e.detail.value
    })
  },
  //搜索
  onSearch: function () {
    let self = this;
    getSearchInfo(this.data.searchVal, this.data.item.id, (r) => {
      if (r.code == 0) {
        self.setData({
          list: r.data
        })

      } else {
        errorMsg(r.message)
      }
    })
  },
//跳转至添加商品
  goAddProduct:function(){
    wx.navigateTo({
      url: '../addproduct/addproduct?classindex='+this.data.classindex,
    })
  }
})